package cn.thr.ymcc.web.controller;

import cn.thr.ymcc.dto.CourseDto;
import cn.thr.ymcc.jwt.LoginContextUtils;
import cn.thr.ymcc.query.CourseQuery;
import cn.thr.ymcc.result.JsonResult;
import cn.thr.ymcc.result.PageList;
import cn.thr.ymcc.service.ICourseService;
import cn.thr.ymcc.domain.Course;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;

    @Autowired
    private LoginContextUtils loginContextUtils;

    /**
     * 课程发布上线
     * @param courseList
     * @return
     */
    @PostMapping("/onLineCourse")
    public JsonResult onLineCourse(@RequestBody List<Course> courseList){
        return  courseService.onLineCourse(courseList);
    }

    /**
    * 保存和修改公用的
    */
    @PostMapping("/save")
    public JsonResult saveOrUpdate(@RequestBody CourseDto courseDto,HttpServletRequest request){
        if(courseDto.getCourse().getId()!=null){
            courseService.updateById(courseDto);
        }else{
            //获取当前登录用户信息
            String token = request.getHeader(LoginContextUtils.JWT_TOKEN_NAME);
             //要传去service层
            courseDto.getCourse().setLoginId(loginContextUtils.getLoginUserId(token));
            courseDto.getCourse().setLoginUserName(loginContextUtils.getLoginUserName(token));
            courseService.save(courseDto);
        }
        return JsonResult.me();
    }


    /**
     * 课程编辑：回显数据【课程详情、课程市场营销、统计信息、课程所属类别、课程的讲师id集合、课程的资源集合】
     * @param courseId
     * @return
     */
    @GetMapping("/getDetailById/{courseId}")
    public JsonResult getDetailById(@PathVariable("courseId")Long courseId){
        return JsonResult.me(courseService.getDettailById(courseId));
    }
    /**
    * 删除对象
    */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        courseService.removeById(id);
        return JsonResult.me();
    }

    /**
   * 获取对象
   */
    @GetMapping("/{id}")
    public JsonResult get(@PathVariable("id")Long id){
        return JsonResult.me().setData(courseService.getById(id));
    }


    /**
    * 查询所有对象
    */
    @GetMapping("/list")
    public JsonResult list(){
        return JsonResult.me().setData(courseService.list(null));
    }


    /**
    * 带条件分页查询数据
    */
    @PostMapping("/pagelist")
    public JsonResult page(@RequestBody CourseQuery query){
        Page<Course> page = new Page<Course>(query.getPageNo(), query.getPageSize());
        //StringUtils.hasText  如果字符串里面的值为null， ""， "  "，那么返回值为false；否则为true
        //where name like  %Keyword%  内部时是个单分支语句，条件为空的时候才拼接这个查询条件
        QueryWrapper<Course> name = new QueryWrapper<Course>().like(StringUtils.hasText(query.getKeyword()), "name", query.getKeyword())
                //orderBy 看是升序还是降序   StringUtils.hasText 判断query.getOrderField() 是否为空，不为空就是升序
                //equalsIgnoreCase 忽略大小写
                //query.getOrderField()排序的字段
                .orderBy(StringUtils.hasText(query.getOrderField()), "asc".equalsIgnoreCase(query.getOrderType()), query.getOrderField());
        page = courseService.page(page,name);
        return JsonResult.me().setData(new PageList<Course>(page.getTotal(), page.getRecords()));
    }



}
